﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GameSound;

namespace MainGame.Menu
{

  public interface IIntroMenu
  {
    event MenuEvent OnStartGame;
    event MenuEvent OnExit;
  }

  public class IntroMenu : BaseMenu, IIntroMenu
  {    
    static int SECONDS = 3; // Durata Intro
    public bool Animated = false;
    SpriteBatch spriteBatch;
    Texture2D Logo_Texture;
    Texture2D Cards_Texture; // Animazione Carte Rotanti
    SoundEffect LogoSound;

    public IntroMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      spriteBatch = Game.GetContext().SpriteBatch;

      Logo_Texture = game.Content.Load<Texture2D>(@"Images//Menu//Logo");

      LogoSound = Game.Content.Load<SoundEffect>(@"Sounds//m_Logo");

      bounds = new Rectangle(0, 0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
      game.Services.AddService(typeof(IIntroMenu), this);

      // Animazione Carte Rotanti
      Cards_Texture = game.Content.Load<Texture2D>(@"Images//Decks//Deck_Intro");

    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(IIntroMenu));
      base.Dispose(disposing);
    }

    String message = Locale.Strings.IntroStr00;
    Vector2 messDim;
    SoundEffect s_logo;
    
    public override void Initialize()
    {
      color.A = 0;

      Random rnd = new Random();
      int a = rnd.Next(DateTime.Now.Second);

      if (a > 0) message = ":-)";
      if (a > 5) message = String.Format(Locale.Strings.IntroStr01, DateTime.Now.ToString("HH"), DateTime.Now.ToString("mm"));
      if (a > 10) message = Locale.Strings.IntroStr02.Replace("\\n", "\n");
      if (a > 15) message = Locale.Strings.IntroStr03;
      if (a > 20) message = Locale.Strings.IntroStr04.Replace("\\n", "\n");
      if (a > 25) message = Locale.Strings.IntroStr05;
      if (a > 30) message = Locale.Strings.IntroStr06;
      if (a > 35) message = "♥ ♦ ♣ ♠";
      if (a > 40) message = Locale.Strings.IntroStr07;
      if (a > 45) message = Locale.Strings.IntroStr08;
      if (a > 50) message = Locale.Strings.IntroStr09;

      messDim = Game.Content.Load<SpriteFont>("small_font").MeasureString(message);

      //var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
      //SoundManager.PlaySound(MenuSounds.Logo, 0.4f, 0.5f, 0.0f);
      LogoSound.Play(0.4f, 0.5f, 0.0f);

      // Animazione Carte Rotanti
      centerBounds = new Vector2(bounds.Width / 2, bounds.Height / 2);
      rotation = (float) (Math.PI * 2) / (a + 1);
      rotation2 = (float)(Math.PI * 3) / 2 / (a + 1);

      base.Initialize();
    }

    float time = 0;

    public override void Update(GameTime gameTime)
    {
      // Input: back button pressed
      if (((Game1)Game).IsBackButtonPressed())
      {
        if (OnExit != null)
          OnExit();
      }

      if (Animated)
      {
        color.A++;
        if (color.A > 100) color2.A += (byte)2;

        time += gameTime.dt();

        if (time >= SECONDS)
        {
          if (OnStartGame != null)
            OnStartGame();
        }
      }

      base.Update(gameTime);
    }

    Color color = Color.White;
    Color color2 = Color.White;

    // Animazione Carte Rotanti
    Vector2 centerBounds;
    private float rotation = 0.0f, rotation2 = 0.0f;
    private Color colorCards = Color.GhostWhite;

    public override void Draw(GameTime gameTime)
    {
      spriteBatch.Begin();

      // Sfondo nero
      spriteBatch.Draw(Cards_Texture, new Rectangle(0, 0, bounds.Width, bounds.Height), new Rectangle(10,10,1,1), Color.Black);
     
      // Animazione Carte Rotanti
      rotation -= 0.01f;
      rotation2 -= 0.02f;
      //colorCards.A--;
      for (int i = 0; i < 13; i++)
      {
        spriteBatch.Draw(Cards_Texture, new Rectangle((bounds.Width / 2), (bounds.Height / 2), Cards_Texture.Width / 13, Cards_Texture.Height / 4), new Rectangle((Cards_Texture.Width / 13) * i, 0, Cards_Texture.Width / 13, 0), Color.White, rotation + i, centerBounds, SpriteEffects.None, 0.01f);
        spriteBatch.Draw(Cards_Texture, new Rectangle((bounds.Width / 2) - 5, (bounds.Height / 2) - 20, Cards_Texture.Width / 13, Cards_Texture.Height / 4), new Rectangle((Cards_Texture.Width / 13) * i, (Cards_Texture.Height / 4), Cards_Texture.Width / 13, Cards_Texture.Height / 4), colorCards, rotation2 + i, centerBounds, SpriteEffects.None, 0.02f);
        spriteBatch.Draw(Cards_Texture, new Rectangle((bounds.Width / 2) - 10, (bounds.Height / 2) + 70, Cards_Texture.Width / 13, Cards_Texture.Height / 4), new Rectangle((Cards_Texture.Width / 13) * i, (Cards_Texture.Height / 4) * 2, Cards_Texture.Width / 13, Cards_Texture.Height / 4), colorCards, rotation2 + i + 2, centerBounds, SpriteEffects.None, 0.03f);
        spriteBatch.Draw(Cards_Texture, new Rectangle((bounds.Width / 2) - 15, (bounds.Height / 2), Cards_Texture.Width / 13, Cards_Texture.Height / 4), new Rectangle((Cards_Texture.Width / 13) * i, (Cards_Texture.Height / 4) * 3, Cards_Texture.Width / 13, Cards_Texture.Height / 4), colorCards, rotation - (1 /(i +1)), centerBounds, SpriteEffects.None, 0.04f);
      }

      // Logo
      spriteBatch.Draw(Logo_Texture, new Rectangle(290, 20, Logo_Texture.Width, Logo_Texture.Height), color);
      
      // Frase
      spriteBatch.DrawString(Game.Content.Load<SpriteFont>("small_font"), message, new Vector2((bounds.Width / 2) - (messDim.X / 2)-1, 379), Color.Gray, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
      spriteBatch.DrawString(Game.Content.Load<SpriteFont>("small_font"), message, new Vector2((bounds.Width / 2) - (messDim.X / 2), 380), color2, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

      spriteBatch.End();
      
      base.Draw(gameTime);
    }

    public event MenuEvent OnStartGame;
    public event MenuEvent OnExit;
  }
}
